$(function () {
    new PasswordStrength('txtbPassword','passStrength');
    $("#txtbValidation").on("keydown", keyDownSearch);//回车登录支持
    $("#imgVerify").click();

    // Validation
    $("#login-form").validate({
        // Rules for form validation
        rules:
            {
                txtbUserName: { required: true ,minlength:2,maxlength:12},
                txtbPassword: { required: true , minlength:6,maxlength:12},

                //txtbValidation: { required: true }
            },
        messages:
            {
                txtbUserName: { required: "用户名不能为空",minlength:"用户名至少2位",maxlength:"密码最多12位"},
                txtbPassword: { required: "密码不能为空",minlength:"密码至少6位",maxlength:"密码最多12位"},
            },

        // Do not change code below
        errorPlacement: function (error, element) {
            error.insertAfter(element.parent());
        }
    });
});

function login() {
    if ($("#login-form").valid()) {
        var loginName = $("#txtbUserName").val();
        var password = $("#txtbPassword").val();   //登录用户的密码


        $.ajax({
            url: '/api/user/login',
            type: 'POST',
            cache: false,
            data: { name: loginName, password: password},
            success: function (res) {
                switch (res.code) {
                    case 200:
                        location.href = "/device/index";
                        break;
                    case -1:
                        alert(res.msg);
                        //清空密码以及验证码栏
                        clearInput();
                        break;
                }
            }
        });
    }

}//end login


//清空密码以及验证码
function clearInput() {
    $("#txtbPassword").val('');   //登录用户的密码
    $("#txtbValidation").val('');  //验证码
}

function PasswordStrength(passwordID,strengthID){
    this.init(strengthID);
    var _this = this;
    document.getElementById(passwordID).onkeyup = function(){
        _this.checkStrength(this.value);
    };
}

PasswordStrength.prototype.init = function(strengthID){
    var id = document.getElementById(strengthID);
    var div = document.createElement('div');
    var strong = document.createElement('strong');
    this.oStrength = id.appendChild(div);
    this.oStrengthTxt = id.parentNode.appendChild(strong);
};

PasswordStrength.prototype.checkStrength = function (val){
    var aLvTxt = ['低','弱','中','高'];
    var lv = 0;
    if(val.match(/[a-z]/g)){lv++;}
    if(val.match(/[0-9]/g)){lv++;}
    if(val.match(/[/@#$%&*._]/g)){lv++;}
    if(val.length < 6){lv=0;}
    if(lv > 3){lv=3;}
    this.oStrength.className = 'strengthLv' + lv;
    this.oStrengthTxt.innerHTML = aLvTxt[lv];
    $("#passStrength").css("display","block");

};

function keyDownSearch(e) {
    // 兼容FF和IE和Opera
    var theEvent = e || window.event;
    var code = theEvent.keyCode || theEvent.which || theEvent.charCode;
    if (code == 13) {

        login();//具体处理函数
        return false;
    }
    return true;
}

if (window!=top){ // 判断当前的window对象是否是top对象
    top.location.href =window.location.href; // 如果不是，将top对象的网址自动导向被嵌入网页的网址
}